package com.cy.server.controller;


import com.cy.server.dto.PositionDto;
import com.cy.server.pojo.Position;
import com.cy.server.pojo.RespBean;
import com.cy.server.service.IPositionService;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author LiShaoZhuo
 * @since 2021-04-26
 */
@RestController
@RequestMapping("/system/basic/pos")
public class PositionController {

    @Autowired
    IPositionService positionService;

    @ApiOperation("查询所有职位信息")
    @PostMapping("/list")
    public RespBean getAllPositions(@RequestBody PositionDto positionDto){
        PageInfo<Position> pageInfo = positionService.selectAllPositions(positionDto);
        return RespBean.success(pageInfo);
    }

    @ApiOperation("新增职位信息")
    @PostMapping("/add")
    public RespBean addPositions(@RequestBody Position position){
        if (positionService.save(position)){
            return RespBean.success("添加成功");
        }
        return RespBean.error("添加失败");
    }

    @ApiOperation("更新职位信息")
    @PutMapping("/update")
    public RespBean updatePosition(@RequestBody Position position){
        if (positionService.updateById(position)){
            return RespBean.success("更新成功");
        }
        return RespBean.error("更新失败");
    }

    @ApiOperation("删除职位信息")
    @DeleteMapping("/delete/{id}")
    public RespBean deletePosition(@PathVariable Integer id){
        if (positionService.removeById(id)){
            return RespBean.success("删除成功");
        }
        return RespBean.error("删除失败");
    }

    @ApiOperation("批量删除职位信息")
    @DeleteMapping("/delete")
    public RespBean deletePositionByIds(Integer[] ids){
        if (positionService.removeByIds(Arrays.asList(ids))){
            return RespBean.success("批量删除成功");
        }
        return RespBean.error("批量删除失败");
    }
}
